package com.cbw.firegame.product.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbw.common.vo.*;
import com.cbw.firegame.product.entity.GameImagesEntity;
import com.cbw.firegame.product.feign.OrderFeignService;
import com.cbw.firegame.product.service.CategoryService;
import com.cbw.firegame.product.service.GameImagesService;
import com.cbw.firegame.product.service.GameInfoService;
import com.cbw.firegame.product.service.GameService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    GameService gameService;
    @Autowired
    GameInfoService gameInfoService;
    @Autowired
    GameImagesService gameImagesService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    OrderFeignService orderFeignService;


    @GetMapping(value = {"/","index.html"})
    public String index(Model model){
        List<CategoryEntity> list = categoryService.list();
        List<GameEntity> gamelist= gameService.getHotGame();
      model.addAttribute("hotgames",gamelist);
        model.addAttribute("categorys",list);
        return "index";
    }

    @GetMapping("/category/gameList/{catId}")
    public String getCategoryGamelist(@RequestParam(value = "courrent",defaultValue = "1")Long courrent,@PathVariable("catId")Long catId, Model model){
        List<CategoryEntity> list = categoryService.list();
        CategoryEntity cateOne = categoryService.getById(catId);
        IPage<GameEntity> page = gameService.page(new Page<>(courrent, 3), new QueryWrapper<GameEntity>().eq("status", 1).eq("cat_id", catId));
        model.addAttribute("page",page);
        model.addAttribute("categorys",list);
        model.addAttribute("catName",cateOne.getCatName());
        return "list";
    }
    @GetMapping("/game/search")
    public String getSearchGame(@RequestParam(value = "courrent",defaultValue = "1")Long courrent,@RequestParam(value = "key",required = false)String key,Model model){
        List<CategoryEntity> list = categoryService.list();
        model.addAttribute("categorys",list);
        QueryWrapper<GameEntity> wrapper = new QueryWrapper<GameEntity>().eq("status", 1);

        IPage<GameEntity> page=null;
        if (!StringUtils.isEmpty(key)){
                wrapper.like("game_name",key).or().like("game_title",key);
        }
        page  = gameService.page(new Page<>(courrent, 3), wrapper);
        model.addAttribute("page",page);
        model.addAttribute("key",key);
        return "list";

    }

    @GetMapping("/game/gamedetail/{gameId}")
    public String getGameInfo(@PathVariable("gameId")Long gameId, Model model, HttpSession session){
        List<CategoryEntity> list = categoryService.list();
        model.addAttribute("categorys",list);

        GameEntity game = gameService.getById(gameId);
        GameInfoEntity gameInfo = gameInfoService.getById(game.getGameDetailId());
        List<GameImagesEntity> images = gameImagesService.list(new QueryWrapper<GameImagesEntity>().eq("game_id", gameId));
        model.addAttribute("game",game);
        model.addAttribute("gameInfo",gameInfo);
        model.addAttribute("images",images);
        MemberEntity loginUser = (MemberEntity) session.getAttribute("loginUser");
        if (loginUser!=null){
            Boolean flag = orderFeignService.getOrderByMemberId(loginUser.getId(), gameId);
            model.addAttribute("flag",flag);
        }
        return "item";
    }





}
